﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class UpdateVip : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var dba = new RDBEngine();
            ListLevel(dba);

            RbSex.Items.Add(new ListItem { Text = "男", Value = CVip.SEX_MALE.ToString() });
            RbSex.Items.Add(new ListItem { Text = "女", Value = CVip.SEX_FEMALE.ToString() });
            RbSex.Items.Add(new ListItem { Text = "未知", Value = CVip.SEX_UNKNOWN.ToString() });
            RbSex.SelectedIndex = 0;

            var lev = Request["lev"];
            if (CharUtil.IsValidateHash(lev))
            {
                LbLevel.SelectedValue = lev;
                CbLevel.SelectedValue = lev;
            }
            else
            {
                LbLevel.SelectedIndex = 0;
                CbLevel.SelectedValue = "0";
            }

            var vip = Request["vip"];
            if (CharUtil.IsValidateHash(vip))
            {
                dba.Reset();
                ReadVip(dba, user.CorpId, vip);
            }
        }

        #region 事件处理
        protected void BtUpdateVip_Click(object sender, EventArgs e)
        {
            SaveVip();
        }

        protected void BtCancelVip_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/Vips.aspx?lev=" + LbLevel.SelectedValue);
        }
        #endregion

        private void ListLevel(RDBEngine dba)
        {
            dba.AddTable(DCustomerLevel.TABLE);
            dba.AddColumn(DCustomerLevel.ID);
            dba.AddColumn(DCustomerLevel.NAME);
            dba.AddSort(DCustomerLevel.POINT, true);

            var dt = dba.ExecuteSelect();
            LbLevel.DataSource = dt;
            LbLevel.DataValueField = DCustomerLevel.ID;
            LbLevel.DataTextField = DCustomerLevel.NAME;
            LbLevel.DataBind();

            CbLevel.DataSource = dt;
            CbLevel.DataValueField = DCustomerLevel.ID;
            CbLevel.DataTextField = DCustomerLevel.NAME;
            CbLevel.DataBind();
        }

        private void ReadVip(RDBEngine dba, string corp, string vip)
        {
            dba.AddTable(DVip.TABLE);
            dba.AddWhere(DVip.CORP_ID, corp);
            dba.AddWhere(DVip.ID, vip);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HdId.Value = vip;
            LlCode.Text = "" + row[DVip.CODE];
            TbLastName.Text = "" + row[DVip.LAST_NAME];
            TbFirstName.Text = "" + row[DVip.FIRST_NAME];
            RbSex.SelectedValue = "" + row[DVip.SEX];
            CbLevel.SelectedValue = "" + row[DVip.LEVEL_ID];
            HfLevel.Value = "" + row[DVip.LEVEL_ID];
            TbPoint.Text = "" + row[DVip.POINT];
            TbCardType.Text = "" + row[DVip.CARD_TYPE];
            TbCardTime.Text = "" + row[DVip.CARD_TIME];
            TbBirthday.Text = "" + row[DVip.BIRTHDAY];
            TbQQ.Text = "" + row[DVip.QQ];
            TbContact.Text = "" + row[DVip.CONTACT];
            TbCellPhone.Text = "" + row[DVip.CELLPHONE];
            TbTelephone.Text = "" + row[DVip.TELEPHONE];
            TbAddr.Text = "" + row[DVip.ADDRESS];
            CbStatus.Checked = (CVip.STATUS_DISABLED == (int)row[DVip.STATUS]);
        }

        private void SaveVip()
        {
            var lastName = TbLastName.Text.Trim();
            if (!WebUtil.IsValid(lastName, "姓", CVip.LAST_NAME, LlErr))
            {
                return;
            }
            var firstName = TbFirstName.Text.Trim();
            if (!WebUtil.IsValid(firstName, "名", CVip.FIRST_NAME, LlErr))
            {
                return;
            }

            var sex = RbSex.SelectedValue;
            if (string.IsNullOrWhiteSpace(sex))
            {
                LlErr.Text = "请选择会员性别！";
                return;
            }

            var point = TbPoint.Text;
            if (!WebUtil.IsValid(point, "会员积分", LlErr))
            {
                return;
            }

            var level = CbLevel.SelectedItem;

            var id = HdId.Value;
            var user = WebUtil.CurrentUser(Session);

            var dba = new RDBEngine();

            bool isUpdate = CharUtil.IsValidateHash(id);
            string code = "";
            if (!isUpdate)
            {
                code = KeyUtil.NextVipKey(dba, user.CorpId);
                dba.Reset();
            }

            dba.AddTable(DVip.TABLE);
            dba.AddParam(DVip.CORP_ID, user.CorpId);
            dba.AddParam(DVip.CHANNEL, CVip.CHANNEL);
            dba.AddParam(DVip.LAST_NAME, lastName);
            dba.AddParam(DVip.FIRST_NAME, firstName);
            dba.AddParam(DVip.DISPLAY_NAME, lastName + firstName);
            dba.AddParam(DVip.SEX, RbSex.SelectedValue, false);
            dba.AddParam(DVip.CELLPHONE, TbCellPhone.Text.Trim());
            dba.AddParam(DVip.CARD_TYPE, TbCardType.Text.Trim());
            dba.AddParam(DVip.CARD_TIME, TbCardTime.Text.Trim());
            dba.AddParam(DVip.TELEPHONE, TbTelephone.Text.Trim());
            dba.AddParam(DVip.QQ, TbQQ.Text.Trim());
            dba.AddParam(DVip.CONTACT, TbContact.Text.Trim());
            dba.AddParam(DVip.POINT, point);
            dba.AddParam(DVip.PROV, "");
            dba.AddParam(DVip.CITY, "");
            dba.AddParam(DVip.AREA, "");
            dba.AddParam(DVip.ADDRESS, TbAddr.Text.Trim());
            dba.AddParam(DVip.POSTCODE, "");
            dba.AddParam(DVip.BIRTHDAY, TbBirthday.Text.Trim());
            dba.AddParam(DVip.REMIND, 0);
            dba.AddParam(DVip.STATUS, CbStatus.Checked ? CVip.STATUS_DISABLED : CVip.STATUE_ENABLED);
            dba.AddParam(DVip.UPDATE_TIME, DVip.NOW, false);
            if (isUpdate)
            {
                if (CbLevel.SelectedValue != HfLevel.Value)
                {
                    dba.AddParam(DVip.LEVEL_ID, level.Value);
                    dba.AddParam(DVip.LEVEL_NAME, level.Text);
                    dba.AddParam(DVip.LEVEL_TIME, DVip.NOW, false);
                }
                dba.AddWhere(DVip.ID, id);
                dba.ExecuteUpdate();

                Response.Redirect("~/POS/Vips.aspx?lev=" + LbLevel.SelectedValue);
            }
            else
            {
                id = HashUtil.UtcTimeInHex();
                dba.AddParam(DVip.ID, id);
                dba.AddParam(DVip.CODE, code);
                dba.AddParam(DVip.LEVEL, level.Value == "1000000000000000" ? 0 : 1);
                dba.AddParam(DVip.LEVEL_ID, level.Value);
                dba.AddParam(DVip.LEVEL_NAME, level.Text);
                dba.AddParam(DVip.LEVEL_TIME, DVip.NOW, false);
                dba.AddParam(DVip.CREATE_TIME, DVip.NOW, false);
                dba.ExecuteInsert();

                ShowDef();
            }
        }

        private void ShowDef()
        {
            HdId.Value = "";
            LlCode.Text = "";
            TbLastName.Text = "";
            TbFirstName.Text = "";
            RbSex.SelectedIndex = 0;
            TbBirthday.Text = "";

            CbLevel.SelectedIndex = 0;
            HfLevel.Value = "";
            TbPoint.Text = "0";
            TbCellPhone.Text = "";
            TbTelephone.Text = "";
            TbAddr.Text = "";

            TbCardType.Text = "";
            TbCardTime.Text = "";
            TbQQ.Text = "";
            TbContact.Text = "";
        }
    }
}